﻿using System.Collections.Generic;
using BakalarskaPraca.Forms;
using BakalarskaPraca.Generation.Data;
using BakalarskaPraca.Generation.Helpers;
using BakalarskaPraca.Generation.Other;
using BakalarskaPraca.Generation.Tasks.Abstract;
using BakalarskaPraca.Interfaces;
using System.Linq;
using ColladaDotNet.Geometry;

namespace BakalarskaPraca.Generation.Tasks
{
    public class HumidityShadowCasting : GenerationProcess
    {
        public int SunSimulationIterations;

        public HumidityShadowCasting(List<HumidityPolygon> polygons, ModelData modelData) : base(polygons, modelData)
        {
            this.SunSimulationIterations = Properties.Settings.Default.SunSimulationIterations;
        }

        public override void Run(IProgressReporter reporter)
        {
            List<GenerationTarget> targets = new List<GenerationTarget>(this.Polygons.Select(polygon => new GenerationTarget(polygon, this.SunSimulationIterations)));
            SunTrail sunTrail = new SunTrail(this.SunSimulationIterations);
            for (int i = 0; i < this.SunSimulationIterations; i++)
            {
                this.ModelData.Scene.Geometries.ForEach(
                    g => g.Polygons.ForEach(
                        poly =>
                        targets.ForEach(
                            t => {if (!t.Equals(poly))
                            {
                                t.DropShadow(poly, sunTrail.GetLightDirection(i));
                            }
                        })));
                targets.ForEach(t => t.ApplyShadowLayer());
                reporter.ReportProgress(i, this.SunSimulationIterations);
            }
            targets.ForEach(t => t.GetHumidity());
            Polygons.Clear();
            Polygons.AddRange(targets.Cast<HumidityPolygon>());
        }

        public override string ToString()
        {
            return "Humidity: Casting Shadows";
        }
    }
}
